import 'package:equatable/equatable.dart';
import 'package:backend.core/core/domain/entities/api/web/api_web_one_time_purchased_item.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_one_time_purchased_item_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_one_time_purchased_item_transaction_dto_factory.dart';
import 'package:backend.purchases/infrastructure/dtos/api/web/api_web_purchased_item_dto.dart';

const kApiWebOneTimePurchasedItemRevokedKey = 'revoked';
const kApiWebOneTimePurchasedItemTransactionIdKey = 'transaction_id';
const kApiWebOneTimePurchasedItemTransactionKey = 'transaction';
const kApiWebOneTimePurchasedItemTypeValue = 'one_time_purchased_item';

class ApiWebOneTimePurchasedItemDto extends Equatable implements ApiWebPurchasedItemDto {
  const ApiWebOneTimePurchasedItemDto._internal({
    required this.purchaseItemSku,
    required this.transactionDto,
    required this.test,
    required this.transactionId,
    required this.revoked,
  });

  @override
  final String purchaseItemSku;

  @override
  final bool test;

  final String transactionId;
  final bool revoked;
  final ApiWebOneTimePurchasedItemTransactionDto transactionDto;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      transactionDto,
      test,
      transactionId,
      revoked,
    ];
  }

  @override
  String get type => kApiWebOneTimePurchasedItemTypeValue;

  factory ApiWebOneTimePurchasedItemDto.fromEntity(
      ApiWebOneTimePurchasedItem oneTimePurchasedItem) {
    final transactionDto = ApiWebOneTimePurchasedItemTransactionDtoFactory().fromEntity(
        oneTimePurchasedItem.transaction);

    return ApiWebOneTimePurchasedItemDto._internal(
      purchaseItemSku: oneTimePurchasedItem.purchaseItemSku,
      transactionDto: transactionDto,
      transactionId: oneTimePurchasedItem.transactionId,
      test: oneTimePurchasedItem.test,
      revoked: oneTimePurchasedItem.revoked,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      kApiWebPurchasedItemTypeKey: type,
      kApiWebPurchasedItemPurchaseItemSkuKey: purchaseItemSku,
      kApiWebPurchasedItemTestKey: test,
      kApiWebOneTimePurchasedItemTransactionIdKey: transactionId,
      kApiWebOneTimePurchasedItemRevokedKey: revoked,
      kApiWebOneTimePurchasedItemTransactionKey: transactionDto.toJson(),
    };
  }
}
